package com.stand.hardcode.manager;

import java.util.*;

import javax.jdo.*;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.stand.hardcode.database.*;
import com.stand.hardcode.entity.*;

public class ItemManager {
	public Item createItem(String title, String description, double price, int status, String photoURL, int quantity, Date expiryDate, String creator) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction txn = pm.currentTransaction();
		
		Item returnItem = null;
		
		try {
			txn.begin();
			
			Item item = new Item();
			item.setTitle(title);
			item.setDescription(description);
			item.setPrice(price);
			item.setStatus(status);
			item.setPhotoURL(photoURL);
			item.setQuantity(quantity);
			item.setExpiryDate(expiryDate);
			item.setCreator(creator);
			
			item = pm.makePersistent(item);
			
			txn.commit();
			
			returnItem = pm.detachCopy(item);
		} catch (Exception e) {
			if (txn.isActive())
			{
				txn.rollback();
			}
			
			returnItem = null;
		}
		finally {
			pm.close();
		}
		
		return returnItem;
	}
	
	public Item getItem(long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Item returnItem = null;

		try {
			Key key = KeyFactory.createKey(Item.class.getSimpleName(), id);

			Item product = pm.getObjectById(Item.class, key);

			Item detached = pm.detachCopy(product);

			returnItem = detached;
		} catch (Exception e) {
			e.printStackTrace();
			returnItem = null;
		} finally {
			pm.close();
		}
		
		return returnItem;
	}
	
	public List<Item> getProductsBySeller(String creator) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		List<Item> returnList = null;

		try {
			Query query = pm.newQuery(Item.class);
			query.setFilter("creator == creatorParam");
			query.declareParameters("String creatorParam");
			List<Item> results = (List<Item>) query.execute(creator);

			results.size();

			returnList = results;
		} catch (Exception e) {
			e.printStackTrace();
			returnList = null;
		} finally {
			pm.close();
		}
		
		return returnList;
	}
	
	public List<Item> getProductsBySeller(String creator, int offset,
			int limit) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		List<Item> returnList = null;
		
		try {
			Query query = pm.newQuery(Item.class);
			query.setFilter("creator == creatorParam");
			query.setRange(offset, limit);
			query.declareParameters("String creatorParam");
			List<Item> results = (List<Item>) query.execute(creator);

			results.size();

			returnList = results;
		} catch (Exception e) {
			e.printStackTrace();
			returnList = null;
		} finally {
			pm.close();
		}
		
		return returnList;
	}
	
	public boolean updateItem(Item item) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction txn = pm.currentTransaction();
		
		boolean returnVal = false;
		
		try {
			txn.begin();
			
			pm.makePersistent(item);
			
			txn.commit();
			
			returnVal = true;
		} catch (Exception e) {
			e.printStackTrace();
			if (txn.isActive()) {
				txn.rollback();
			}
			
			returnVal = false;
		} finally {
			pm.close();
		}
		
		return returnVal;
	}
	
	public boolean deleteItem(Item item) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction txn = pm.currentTransaction();
		
		boolean returnVal = false;
		
		try {
			if (doesItemExist(item)) {
				txn.begin();
				
				pm.deletePersistent(item);
				
				txn.commit();
				
				returnVal = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (txn.isActive()) {
				txn.rollback();
			}
			returnVal = false;
		} finally {
			pm.close();
		}
		
		return returnVal;
	}
	
	public boolean doesItemExist(Item item) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		boolean returnVal = false;
		
		try {
			if (item.getKey() != null) {
				Item itemObj = pm.getObjectById(Item.class, item.getKey());
				if (itemObj != null) {
					returnVal = true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return returnVal;
	}
}
